package com.kb.design.controller;

import com.kb.design.common.ApiResponse;
import com.kb.design.dto.Children;
import com.kb.design.entity.DesignDept;
import com.kb.design.entity.DesignPost;
import com.kb.design.service.CustomContactService;
import com.kb.design.service.DeptService;
import com.kb.design.service.PostService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门信息
 * 
 * @author czx
 */
@RestController
@RequestMapping("/post")
public class PostController {
    @Resource
    private PostService postService;

    @PostMapping("/list")
    public ApiResponse list(@RequestBody DesignPost post)
    {
        return ApiResponse.success(postService.selectDeptList(post));
    }
    @GetMapping("/getPostById")
    public ApiResponse getPostById(Long id)
    {
        return ApiResponse.success( postService.getPostById(id));
    }
    /**
     * 新增保存部门
     */

    @PostMapping("/add")
    public ApiResponse addSave(@RequestBody @Validated DesignPost post)
    {
        Long deptId = post.getPostId();
//        deptService.checkDeptDataScope(deptId);
        if (!postService.checkPostNameUnique(post))
        {
            return ApiResponse.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        postService.insertPost(post);
        return ApiResponse.success("新增成功");
    }
    @GetMapping("/remove")
    public ApiResponse remove(Long postId)
    {
        try {
            postService.deletePostById(postId);
            return  ApiResponse.success("删除成功");
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    /**
     * 修改保存部门
     */
    @PostMapping("/updateDept")
    public ApiResponse editSave(@RequestBody DesignPost dept)
    {

        return ApiResponse.success(postService.updatePost(dept));
    }

    /**
     * 查询岗位人员信息
     */
    @PostMapping("/queryPostUser")
    public Map queryPostUser()
    {
        Map map = new HashMap();
        Children children = postService.queryPostUser();
        ArrayList<Children> objects = new ArrayList<>();
        objects.add(children);
        map.put("options",objects);
        return map;
//        return ApiResponse.success(postService.queryPostUser());
    }

}
